package com.wkbb.sports.controller;

import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.sports.dto.HlifeTeachCourseDto;
import com.wkbb.sports.dto.PhysicalAnalysisDto;
import com.wkbb.sports.model.HlifeTeachCourse;
import com.wkbb.sports.model.HlifeTeachCourseAttend;
import com.wkbb.sports.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(description = "课堂")
@RequestMapping("/sports/{apiVersion}/course")
@Slf4j
public class CourseController extends BaseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "开始授课 or 结束授课")
    @RequestMapping(value = "/putCourse", method = RequestMethod.POST)
    public ResultDTO<HlifeTeachCourseDto> putCourse(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @RequestBody HlifeTeachCourse course) {
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        course.setTeacherId(userDto.getId());
        course.setTeacherName(userDto.getName());
        HlifeTeachCourseDto dto = courseService.putCourse(course);
        return dto != null ? new ResultDTO<>(dto) : new ResultDTO<>(ErrorCode.SYSTEM_ERROR);
    }

    @ApiOperation(value = "获取授课详情")
    @RequestMapping(value = "/findCourse", method = RequestMethod.GET)
    public ResultDTO<?> findCourse(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "课程ID", required = false) @RequestParam(name = "courseId", required = false) Long courseId,
            @ApiParam(value = "班级ID") @RequestParam(name = "classId", required = false) Long classId,
            @ApiParam(value = "教学计划ID") @RequestParam(name = "teachPlanDetailId", required = false) Long teachPlanDetailId){
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        HlifeTeachCourse hlifeTeachCourse = new HlifeTeachCourse();
        hlifeTeachCourse.setId(courseId);
        hlifeTeachCourse.setClassId(classId);
        hlifeTeachCourse.setTeachPlanDetailId(teachPlanDetailId);
        HlifeTeachCourseDto dto = courseService.findCourse(hlifeTeachCourse);
        return new ResultDTO<>(dto);
    }

    @ApiOperation(value = "提交课堂总结")
    @RequestMapping(value = "/putCourseSummary", method = RequestMethod.POST)
    public ResultDTO<?> putCourseSummary(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "课程ID", required = true) @RequestParam(name = "courseId", required = true) Long courseId,
            @ApiParam(value = "总结", required = true) @RequestParam(name = "summary", required = true) String summary){
        UserDto userDto = this.getUserByLogin();
        if (null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        boolean b = courseService.putCourseSummary(courseId,summary);
        return getResult(b);
    }

    @ApiOperation(value = "学生签到")
    @RequestMapping(value = "/studentsAttend", method = RequestMethod.POST)
    public ResultDTO<?> studentsAttend(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "课程ID", required = true) @RequestParam(name = "courseId", required = true) Long courseId,
            @RequestBody List<HlifeTeachCourseAttend> attends){
        UserDto userDto = this.getUserByLogin();
        if(null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        boolean b = courseService.studentsAttend(courseId,attends);
        return getResult(b);
    }

    @ApiOperation(value = "老师体质分析")
    @RequestMapping(value = "/getTeacherPhysicalAnalysis", method = RequestMethod.GET)
    public ResultDTO<PhysicalAnalysisDto> getTeacherPhysicalAnalysis(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "班级ID", required = true) @RequestParam(name = "classId", required = true) Long classId)
    {
        UserDto userDto = this.getUserByLogin();
        if(null == userDto) {
            return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
        }
        PhysicalAnalysisDto dto = courseService.getTeacherPhysicalAnalysis(orgId,classId);
        if (dto == null) {
            dto = new PhysicalAnalysisDto();
        }
        dto.format();
        return new ResultDTO<>(dto);
    }

    @ApiOperation(value = "老师测评数据")
    @RequestMapping(value = "/getTeacherStatistics", method = RequestMethod.GET)
    public ResultDTO<Map<String,Object>> getTeacherStatistics(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "组织ID", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "项目类型", required = true) @RequestParam(name = "projectKind", required = true) Integer projectKind)
    {
        try {
            UserDto userDto = this.getUserByLogin();
            if(null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String,Object> map = courseService.getTeacherStatistics(orgId,userDto.getId(),projectKind);
            return new ResultDTO<Map<String,Object>>(map);
        } catch (Exception e) {
            log.error("***********getTeacherTrainWholeStat******", e);
            return new ResultDTO<>(ErrorCode.SYSTEM_EXCEPTION);
        }
    }

}
